﻿// SocketWebAssembly.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<ctime>
#include "SocketWebAssembly.h"
int main()
{

	//#pragma region 变量
	//	const int size = 10;
	//	char* value;
	//	char value2[10] = "sfsa";
	//#pragma endregion
	//	printf("定义长度:%d\r\n", size);
	//	value = GetUnSignChar(size);
	//	char path[30] = "d:\\a.txt";
	//	WriteTxt(value, path, size);
	//
	//	int * intArray = GetIntArray(size);
	//	//printf("字符串长度:%d，类型小大：%d，数组长度：%d\r\n"
	//	//	, strlen((char *)intArray)
	//	//	, sizeof(int)
	//	//	, sizeof(&intArray[0]));
	//	char path2[10] = "d:\\b.txt";
	//	WriteTxtInt(intArray, path2, size);
	//
	//	char path3[10] = "d:\\c.txt";
	//	CopyArray(value, 0, value2, 1, 4);
	//	WriteTxt(value2, path3, 5);
	int taskId = GetRand();
	unsigned char funcCode = 0x43;
	unsigned char operateCode = 0x44;
	unsigned char useType = 0x45;
	bool oddSign = true;
	unsigned char * TaskId = GetTaskId(taskId);
	const int MessageLen = 12;
	unsigned char Content[MessageLen + 1] = "helloworldlh";
	unsigned char BeginSite = 0x02;
	int contentLength = MessageLen;
	unsigned char * RandArray = GetOutMessageByte(
		funcCode
		, operateCode
		, useType
		, TaskId
		, Content
		, contentLength);
	char path3[10] = "d:\\c.txt";
	WriteUnsignedCharTxt(RandArray, path3, contentLength + 14);
	if (checkSite(RandArray, contentLength + 14, true))
	{
		printf("check Ok\r\n");
	}
	unsigned char  * LenArray = (unsigned char*)calloc(4, sizeof(unsigned char));
	CopyArray(RandArray, 1, LenArray, 0, 4);
	int ArrayLen = bytes2Int(LenArray);
	printf("长度：%d\r\n", ArrayLen);

	unsigned char * TaskArray = (unsigned char *)calloc(5, sizeof(unsigned char));
	TaskArray = ReturnTaskId(RandArray);
	printf("任务：%s\r\n", &TaskArray[0]);

	unsigned char * ContentArray = ReturnContent(RandArray);
	printf("内容：");
	for (int i = 0; i < contentLength; i++)
	{
		printf("%c", ContentArray[i]);
	}
	printf("\r\n");

	int indexs = ArrayLen;
	int * CountArray = GetTwoFind(RandArray, &indexs);
	for (int i = 0; i < indexs; i++)
	{
		printf("%d", CountArray[i]);
	}
	free(ContentArray);
	free(TaskArray);
	free(LenArray);
	free(RandArray);
}
///二分法查找
int * GetTwoFind(unsigned char * array, int * length) {
	int * ReturnArray = (int *)calloc(10, sizeof(int));
	int Time = *length / 2;
	int count = 0;
	for (int i = 0; i < Time; i++)
	{
		if (array[i] == 0x02)
			ReturnArray[count++] = i;
		if (array[i + Time] == 0x02)
			ReturnArray[count++] = i + Time;
	}
	if (*length % 2 == 1)
		ReturnArray[count++] = * length - 1;
	* length = count;
	return ReturnArray;
}
void swap_int(int *a, int *b)
{
	int *temp = a;
	a = b;
	b = temp;
}

bool checkSite(unsigned char * array, int length, bool oddSign)
{
	unsigned char *  checkArray = (unsigned char*)calloc(length, sizeof(unsigned char));
	CopyArray(array, 0, checkArray, 0, length - 1);
	int count = getOneCount(checkArray, length - 1);
	if (count % 2 == 0)
	{
		if (oddSign)
		{
			return array[length - 1] == 0x01;
		}
		else
		{
			return array[length - 1] == 0x00;
		}
	}
	else
	{
		if (oddSign)
		{
			return array[length - 1] == 0x00;
		}
		else
		{
			return array[length - 1] == 0x01;
		}
	}
}
int getOneCount(unsigned char * array, int length)
{
	int count = 0;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if ((array[i] >> j & 0x01) == 1)
				count++;
		}
	}
	return count;
}
int bytes2Int(unsigned char * bytes)
{
	int int1 = bytes[0] & 0xff;
	int int2 = (bytes[1] & 0xff) << 8;
	int int3 = (bytes[2] & 0xff) << 16;
	int int4 = (bytes[3] & 0xff) << 24;

	return int1 | int2 | int3 | int4;
}

void WriteTxtInt(int * Array, char *FileName, int size) {
	FILE *fp = fopen(FileName, "w");
	fwrite(Array, sizeof(int), size, fp);
	if (!Array)
		fclose(fp);
}
void WriteUnsignedCharTxt(unsigned char * Array, char *FileName, int size) {
	FILE *fp = fopen(FileName, "w");
	fwrite(Array, sizeof(unsigned char), size, fp);
	if (!Array)
		fclose(fp);
}

void WriteTxt(char * Array, char *FileName, int size) {
	FILE *fp = fopen(FileName, "w");
	fwrite(Array, sizeof(char), size, fp);
	if (!Array)
		fclose(fp);
}
int * GetIntArray(int size) {
	int * value = new int[size];
	for (int i = 0; i < size; i++)
	{
		value[i] = 0x30 + i + 1;
	}
	return value;
}

void CopyArray(unsigned char * source, int sBegin, unsigned char * des, int dBegin, int length) {
	if (length > 0 && sBegin > -1 && dBegin > -1)
	{
		for (int i = 0; i < length; i++)
		{
			des[i + dBegin] = source[i + sBegin];
		}
	}
}
char* GetUnSignChar(int size) {
	char * value = new char[size];
	for (int i = 0; i < size; i++)
	{
		value[i] = 0x41 + i;
	}
	return value;
}

unsigned char getOddSite(unsigned char* array, int length)
{
	return getCheckByte(array, true, length);
}

unsigned char getEvenSite(unsigned char* array, int length)
{
	return getCheckByte(array, false, length);
}

unsigned char getCheckByte(unsigned char * array, bool oddSign, int length)
{
	int count = getOneCount(array, length);
	if (count % 2 == 0)
	{
		return oddSign ? 0x01 : 0x00;
	}
	else
	{
		return oddSign ? 0x00 : 0x01;
	}
}

unsigned char * ReturnTaskId(unsigned char * Array) {
	unsigned char * value = (unsigned char*)calloc(5, sizeof(unsigned char));
	CopyArray(Array, 8, value, 0, 5);
	return value;
}

unsigned char * ReturnContent(unsigned char * Array) {
	unsigned char  * LenArray = (unsigned char*)calloc(4, sizeof(unsigned char));
	CopyArray(Array, 1, LenArray, 0, 4);
	int ArrayLen = bytes2Int(LenArray) - 14;
	unsigned char * Content = (unsigned char*)calloc(ArrayLen, sizeof(unsigned char));
	CopyArray(Array, 13, Content, 0, ArrayLen);
	free(LenArray);
	return Content;
}

unsigned char * GetTaskId(int taskId) {
	unsigned char * value = (unsigned char*)calloc(taskId, sizeof(unsigned char));
	if (taskId > -1 && taskId < 10)
	{
		value[0] = taskId;
	}
	else if (taskId >= 10 && taskId < 100) {
		value[0] = taskId / 10;
		taskId = taskId % 10;
		value[1] = taskId;
	}
	else if (taskId >= 100 && taskId < 1000) {
		value[0] = taskId / 100;
		taskId = taskId % 100;
		value[1] = taskId / 10;
		taskId = taskId % 10;
		value[2] = taskId;
	}
	else if (taskId >= 1000 && taskId < 10000) {
		value[0] = taskId / 1000;
		taskId = taskId % 1000;
		value[1] = taskId / 100;
		taskId = taskId % 100;
		value[2] = taskId / 10;
		taskId = taskId % 10;
		value[3] = taskId;
	}
	else if (taskId >= 10000 && taskId < 100000) {
		value[0] = taskId / 10000;
		taskId = taskId % 10000;
		value[1] = taskId / 1000;
		taskId = taskId % 1000;
		value[2] = taskId / 100;
		taskId = taskId % 100;
		value[3] = taskId / 10;
		taskId = taskId % 10;
		value[4] = taskId;
	}
	else {

	}
	for (int i = 0; i < 5; i++)
	{
		value[i] += 0x30;
	}
	return value;
}
int GetRand() {
	srand(time(0));
	return  rand() % 100000;
}

unsigned char * GetOutMessageByte(unsigned char funcCode
	, unsigned char operateCode
	, unsigned char useType
	, unsigned char * TaskId
	, unsigned char * Content
	, int contentLength)
{
	return GetMessageByte(
		funcCode
		, operateCode
		, useType
		, true
		, TaskId
		, Content
		, 0x02
		, contentLength);
}
unsigned char * GetMessageByte(unsigned char funcCode
	, unsigned char operateCode
	, unsigned char useType
	, bool oddSign
	, unsigned char * TaskId
	, unsigned char * Content
	, unsigned char BeginSite
	, int contentLength)
{
	unsigned char *ContentByte = (unsigned char*)calloc(contentLength + 5, sizeof(unsigned char));
	int Length = 9;
	if (contentLength > 0)
	{
		CopyArray(TaskId, 0, ContentByte, 0, 5);
		CopyArray(Content, 0, ContentByte, 5, contentLength);
		Length += contentLength + 5;
		free(TaskId);
	}
	unsigned char * array = (unsigned char*)calloc(Length, sizeof(unsigned char));
	int index = 0;
	array[index++] = BeginSite;
	array[index++] = (unsigned char)(Length & 0xff);// 最低位
	array[index++] = (unsigned char)((Length >> 8) & 0xff);// 次低位
	array[index++] = (unsigned char)((Length >> 16) & 0xff);// 次高位
	array[index++] = (unsigned char)((Length >> 24) & 0xff);// 最高位,无符号右移。
	array[index++] = funcCode;//加入功能码
	array[index++] = operateCode;//加入转发码
	array[index++] = useType;//加入转发用户类型
	if (contentLength > 0)
	{
		CopyArray(ContentByte, 0, array, index, contentLength + 5);//COPY消息内容到报文体
		index += contentLength + 5;
	}
	free(ContentByte);
	array[index] = oddSign ? getOddSite(array, index) : getEvenSite(array, index);
	return array;
}
